$(document).ready(function () {
    toastr.options = {
        "closeButton": true,
        "debug": false,
        "progressBar": false,
        "positionClass": "toast-top-full-width",
        "onclick": null,
        "showDuration": "400",
        "hideDuration": "1000",
        "timeOut": "7000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    }
    $('#login_btn').click(function () {
        doLogin();
    })

    document.onkeydown=function(event){
        var e = event || window.event || arguments.callee.caller.arguments[0];
        if(e && e.keyCode==13){
            doLogin();
        }
    }
})

function doLogin(){
    var username = $('#login_form input[name=username]').val();
    var password = $('#login_form input[name=password]').val();
    if (username == '') {
        toastr.error('请填写用户名!!');
        $('#login_form input[name=username]').focus();
        return false;
    }
    if (password == '') {
        toastr.error('请填写密码!!');
        $('#login_form input[name=password]').focus();
        return false;
    }
    $.ajax({
        type: "post",
        url: "/admin/public/dologin",
        data: {username:username, password:password},
        dataType: "json",
        timeout:200,
        async:false,
        beforeSend:function(data){
            $('#loading_btn').show(0);
            $('#login_btn').hide(0);
        },
        success: function(data){
            if(data.errno > 0){
                toastr.error(data.errmsg);
                return false;
            }else{
                window.location.href= data.data.url;
            }
        },
        complete:function(data){
            $('#loading_btn').hide(0);
            $('#login_btn').show(0);
        }

    });
}